<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>命令式</title>
</head>

<body>
    <div id="app">
        <p></p>
    </div>
    <script>
        let data = {
            name: 'lotzinfly',
            height: '180',
            age: '22'
        }
        //1.由于有多个属性。我们可以用遍历的方法
        Object.keys(data).forEach((key) => {
            console.log(key, data[key])
            observe(data, key, data[key])
        })
        //2.把对象属性转成get()和set()的形式
        function observe(data, key, value) {
            Object.defineProperty(data, key, {
                get() {
                    console.log('你访问了属性', key)
                    return value

                },

                set(newValue) {
                    //set函数执行，不会判断两次修改是否一样
                    if(newValue===value){
                        return
                    }
                    console.log('你修改了属性', key)
                    value = newValue
                    //核心：操作dom 把最新值赋值上去
                    document.querySelector('#app p').innerText = newValue
                    //只要修改name属性，就会触发set函数执行

                }
            })
        }
        document.querySelector('#app p').innerText = data.name

    </script>
</body>

</html>